package the.workshop.milktea.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.workshop.milktea.infrastructure.mapper.CategoryMapper;
import the.workshop.milktea.infrastructure.model.Category;
import the.workshop.milktea.service.dto.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 种类 服务实现类
 * </p>
 *
 * @author huamu
 * @since 2023-04-01
 */
@Service
public class CategoryService extends AbstractSuperService<CategoryMapper, Category> {

    public boolean add(CategoryAddDTO dto) {
        Category entity = new Category();
        BeanUtils.copyProperties(dto, entity);

        entity.setCreatedId(dto.getOperatorId());
        entity.setCreatedName(dto.getOperatorName());
        entity.setCreatedTime(new Date());
        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        this.baseMapper.insert(entity);
        return true;
    }

    public boolean modify(CategoryModifyDTO dto) {
        Category entity = new Category();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(dto.getCategoryId());

        entity.setModifiedId(dto.getOperatorId());
        entity.setModifiedName(dto.getOperatorName());
        entity.setModifiedTime(new Date());
        this.baseMapper.updateById(entity);
        return true;
    }

    public QueryResult<ProductPageResultDTO> page(ProductPageDTO condition) {
        if (condition.getPageNum() != null || condition.getPageSize() != null) {
            return null;
        }
        PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        Page<ProductPageResultDTO> page = this.baseMapper.selectListPage(condition);
        return new QueryResult(page.getTotal(), page.getResult());
    }

    public CategoryGetDTO get(Integer productId) {
        Category entity = this.baseMapper.selectById(productId);
        if (entity == null) {
            return null;
        }
        CategoryGetDTO dto = new CategoryGetDTO();
        BeanUtils.copyProperties(entity, dto);
        dto.setCategoryId(entity.getId());
        return dto;
    }

    public List<CategoryPageResultDTO> getList() {
        List<Category> categoryList = this.baseMapper.selectList(new LambdaQueryWrapper<>());
        return CollectionUtils.isEmpty(categoryList) ? null : categoryList.stream().map(this::convertOf).collect(Collectors.toList());
    }

    private CategoryPageResultDTO convertOf(Category source) {
        CategoryPageResultDTO target = new CategoryPageResultDTO();
        target.setCategoryId(source.getId());
        target.setName(source.getName());
        return target;
    }

}
